// 应用入口

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.tsx';
import './index.css';

// 在开发环境中加载 AI 测试工具
if (import.meta.env.DEV) {
  import('./utils/aiTestUtils').then((aiTestModule) => {
    const { testAIHealth, testAIChat, testSceneGeneration, testWordDefinition, runFullAITest } = aiTestModule;
    
    // 将测试函数添加到全局对象，供控制台使用
    Object.assign(window, {
      testAIHealth,
      testAIChat, 
      testSceneGeneration,
      testWordDefinition,
      runFullAITest
    });
    
    console.log('🔧 AI 测试工具已加载到开发环境！');
    console.log('在控制台中运行 runFullAITest() 来测试您的 DeepSeek 模型集成');
  });
}

// PWA 支持
if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('/sw.js')
      .then((registration) => {
        console.log('SW registered: ', registration);
      })
      .catch((registrationError) => {
        console.log('SW registration failed: ', registrationError);
      });
  });
}

// 移动端视口设置
const setViewportHeight = () => {
  const vh = window.innerHeight * 0.01;
  document.documentElement.style.setProperty('--vh', `${vh}px`);
};

setViewportHeight();
window.addEventListener('resize', setViewportHeight);

// 禁止移动端双击缩放
document.addEventListener('touchstart', (e) => {
  if (e.touches.length > 1) {
    e.preventDefault();
  }
});

let lastTouchEnd = 0;
document.addEventListener('touchend', (e) => {
  const now = (new Date()).getTime();
  if (now - lastTouchEnd <= 300) {
    e.preventDefault();
  }
  lastTouchEnd = now;
}, false);

// 渲染应用
ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
);